import React from 'react'
import { Form, Input, Button, Checkbox, message } from 'antd';
import { useDispatch } from 'react-redux';
import UserService from "../../model/user/Service"
import { initUser } from '../../action/userAction';

export default (props: any) => {

    const dispatch = useDispatch()
    const onFinish = async (info: any) => {
        let { userid, userpwd } = info;
        let { data } = await UserService.login({ userid, userpwd });
        if (data.code == 1) {
            message.success("登录成功")
            let info = data.token;
            window.localStorage.setItem("userinfo", info);//放在本地
            props.history.push("/menu");
            dispatch(initUser(info))
        } else {
            message.error('用户名和密码不匹配');
        }
    };

    const onFinishFailed = (errorInfo: any) => {
        console.log('Failed:', errorInfo);
    };
    return (
        <div className="loginform">
            <h3>欢迎登录</h3>
            <Form
                name="basic"
                labelCol={{ span: 8 }}
                wrapperCol={{ span: 16 }}
                initialValues={{ remember: true }}
                onFinish={onFinish}
                onFinishFailed={onFinishFailed}
                autoComplete="off"
            >
                <Form.Item
                    label="手机号"
                    name="userid"
                    rules={[{ required: true, pattern: /^1[3-9][0-9]{9}$/, message: '请输入正确的手机号' }]}
                >
                    <Input />
                </Form.Item>
                <Form.Item
                    label="密码"
                    name="userpwd"
                    rules={[{ required: true, pattern: /^[a-z0-9_$]{6,10}$/i, message: '请输入数字字幕_$组成的6-10位的密码' }]}
                >
                    <Input.Password />
                </Form.Item>
                <Form.Item name="remember" valuePropName="checked" wrapperCol={{ offset: 8, span: 16 }}>
                    <Checkbox>我已阅读并同意<a href="" className="yhys">《用户隐私政策》</a></Checkbox>
                </Form.Item>
                <Form.Item wrapperCol={{ offset: 8, span: 16 }}>
                    <Button type="primary" htmlType="submit" className="loginbtn">登录</Button>
                </Form.Item>
            </Form>
        </div>
    )
}
